package finalproject.model.items;
import finalproject.config.Randomizer;
import finalproject.config.Ressource;
import finalproject.model.player.CourseBook;
import finalproject.model.player.ItemManager;
import finalproject.model.player.Player;
import finalproject.model.studies.Lab;
import finalproject.model.studies.Lecture;
import finalproject.view.StringManager;

/**
 * This class is the tablet class of the project "World of Zuul"
 * A tablet is an object that the player can use.
 * When used, the player can learn new lab/lecture, or can plan a game and lose a lecture
 * @author Nicolas Bighetti & Yassine Tijani
 *
 */
public class Tablets extends Item {

	/**
	 * The chance the player has to play on the tablet instead of working
	 */
	private static final double PLAY_CHANCE = 0.50;
	/**
	 * The chance the player has to get a lab instead of a lecture on the tablet
	 */
	private static final double LAB_CHANCE = 0.8;

	/**
	 * the constructor of the tablet class
	 */
	public Tablets()
	{
		super(StringManager.getString("tablet"), StringManager.getString("tabletdescription"), 1, true, false, false);
	}

	/**
	 * The use implementation of the tablet class
	 * the player can either work or play on the tablet
	 * @param player The player using the tablet
	 * @return 
	 */
	@Override
	public void use(Player player) {
		// If the next random double is less than the play chance, the player will work
		if(Randomizer.getRandom().nextDouble() < PLAY_CHANCE)
		{
			work(player);
		}
		else
		{
			play(player);
		}
	}

	/**
	 * The method called by the use method when the player play on the tablet
	 * It will make the player play a game and forget a lecture
	 * @param player the player using the tablet
	 */
	private void play(Player player) {

	    if(Randomizer.getRandom().nextInt(2) == 0)
	    {
		ItemManager manager = player.getItemHolder();
		int randInt = Randomizer.getRandom().nextInt(manager.getWorkingManager().size());
		LectureItem key = manager.getWorkingManager().getKey(randInt);


		manager.getWorkingManager().eraseKey(key);
		player.getBook().removeLectureOf(key.getLecture().getTitle());
		StringManager.outPut("playtablet");
	    }
	    else
	    {
	        StringManager.outPut("zuul");
	    }
	}

	/**
	 * The method called by the use method when the player work on the tablet
	 * On it he will find a lab session or a lecture.
	 * The corresponding item is automatically given to the player
	 * @param player the player using the tablet
	 */
	private void work(Player player) {

		CourseBook playerBook = player.getBook();

		if(player.getEnergy().getEnergy() <= 0)
		{
		    StringManager.outPutText(StringManager.getString("exhausted")); 
		    return;
		}
		//If the next double is less than the lab chance, the player will learn a new lecture
		if(Randomizer.getRandom().nextDouble() < LAB_CHANCE)
		{
			Lecture lect = playerBook.getUndoneCourse(Ressource.listCourse);
			player.getItemHolder().getWorkingManager().addKey(new LectureItem(lect));
			playerBook.getLectures().add(lect);
			player.getEnergy().setEnergy(player.getEnergy().getEnergy()-1);
			StringManager.outPutText(StringManager.getString("coursetab") + ": " + lect.getTitle());
		}
		else
		{
			Lab lab = playerBook.getUndoneLab(Ressource.listLab);
			if(player.getItemHolder().getWorkingManager().containskeyOf(lab.getTitle()))
			{
				player.getItemHolder().getWorkingManager().addValue(new LabItem(lab));
				player.getEnergy().setEnergy(player.getEnergy().getEnergy()-1);
				StringManager.outPutText(StringManager.getString("labtab") + ": " + lab.getTitle());
			}
			else
			{
				StringManager.outPut("nocorrespondinglect");
			}
		}
	}
}
